<template>
  <section>
    <el-form label-width="6rem" ref="outRoom" :model="outData" :loading="loading" :rules="rule">
      <el-form-item label="退宿类型" prop="RY_retreat_type">
        <el-select v-model="outData.RY_retreat_type" placeholder="退宿类型">
          <el-option
            v-for="item in RY_retreat_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="退宿时间" prop="outTime">
        <el-date-picker
          v-model="outData.outTime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions1"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="RY_name">
        <el-input v-model="outData.RY_name" @blur="nameBlur(outData.RY_name)"></el-input>
      </el-form-item>
      <!-- <el-form-item label="身份证号码" prop="RY_idcardnum">
        <el-input v-model="outData.RY_idcardnum" :disabled="true"></el-input>
      </el-form-item>-->
      <el-form-item label="工号" prop="RY_degsonid">
        <el-input v-model="outData.RY_degsonid" :disabled="false"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="RY_sex">
        <el-radio-group v-model="outData.RY_sex" :disabled="true">
          <el-radio label="0">女</el-radio>
          <el-radio label="1">男</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="部门">
        <el-input v-model="outData.RY_department" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="入住位置">
        <el-input v-model="outData.position" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="入住时间">
        <el-input v-model="outData.RY_live_date" :disabled="true"></el-input>
      </el-form-item>
      <!-- <el-form-item label="领用物品退还"> -->
      <!-- <el-select
          v-model="outData.goods"
          multiple
          filterable
          allow-create
          default-first-option
          placeholder="请选择物品"
        >
          <el-option
            v-for="item in goods"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
      </el-select>-->
      <!-- <el-transfer v-model="outData._goods" :data="goods" :titles="['已领用', '已归还']"></el-transfer>
      </el-form-item>-->
      <el-form-item label="备注">
        <el-input v-model="outData.RY_remarks" type="textarea" autosize placeholder="请输入内容"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="resettingSubmit('outRoom')">取消</el-button>
        <el-button @click="outSubmit('outRoom')">提交</el-button>
      </el-form-item>
    </el-form>
    <!-- 费用弹出框 -->
  </section>
</template>
<script>
import { retreat_select, retreat } from "../../api/retreat.js";
export default {
  data() {
    return {
      outData: {
        RY_retreat_type: null,
        RY_degsonid: null,
        outTime: null,
        RY_sex: null,
        RY_name: null,
        RY_department: null,
        RY_live_date: null,
        position: null,
      },
      ghDisabled: true,
      loading: false,
      RY_retreat_type: [
        {
          value: "0",
          label: "离职",
        },
        {
          value: "1",
          label: "自离",
        },
        {
          value: "2",
          label: "调房",
        },
        {
          value: "3",
          label: "外宿",
        },
      ],
      pickerOptions1: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      goods: [],
      // 填写规则
      rule: {
        RY_retreat_type: [
          { required: true, message: "请选择退宿类型", trigger: "change" },
        ],
        // RY_degsonid: [
        //   {
        //     required: true,
        //     message: "请输入工号",
        //     trigger: "blur",
        //   },
        // ],
        outTime: [
          {
            type: "date",
            required: true,
            message: "请选择退宿时间",
            trigger: "change",
          },
        ],
        // RY_name: [
        //   {
        //     required: true,
        //     message: "请输入姓名",
        //     trigger: "blur",
        //   },
        // ],
      },
    };
  },
  methods: {
    // 取消操作
    resettingSubmit(formName) {
      this.$refs[formName].resetFields();
      // this.goods = [];
      // this.area = [];
      // this.dorm = [];
      // this.floor = [];
      // this.room = [];
      // this.bed = [];
    },
    nameBlur(RY_name) {
      // 根据姓名获取
      if (this.outData.RY_name === "") {
      }
      this.loading = true;
      retreat_select({
        RY_degsonid: this.outData.RY_degsonid,
        RY_name: this.outData.RY_name,
      }).then((res) => {
        this.loading = false;
        if (res.data) {
          this.outData.RY_degsonid = res.data[0].RY_degsonid;
          this.outData.RY_sex = res.data[0].RY_sex;
          this.outData.RY_department = res.data[0].RY_department;
          this.outData.RY_number = res.data[0].RY_number;
          this.outData.RY_bed = res.data[0].RY_bed;
          this.outData.RY_live_date = res.data[0].RY_live_date;
          let area = res.data[0].AR_number;
          let floor = res.data[0].SSL_number;
          let room = res.data[0].RY_number;
          let bed = res.data[0].RY_bed;
          this.outData.position = area + "*" + floor + "*" + room + "*" + bed;
        } else {
          this.$confirm("此员工暂未入住", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          });
          // .then(() => {
          //   this.personDisabled = false;
          //   this.outData.SQ_RY_idcardnum = "";
          //   this.outData.SQ_RY_location = "";
          //   this.outData.SQ_RY_sex = "";
          // })
          // .catch(() => {
          //   this.$message({
          //     type: "info",
          //     message: "已取消操作",
          //   });
          // });
        }
      });
    },
    // 提交
    outSubmit(forName) {
      this.$refs[forName].validate((valid) => {
        if (valid) {
          //转换日期输出格式
          var chinaStandard = this.outData.outTime;
          var date = new Date(chinaStandard);
          var y = date.getFullYear();
          var m = date.getMonth() + 1;
          m = m < 10 ? "0" + m : m;
          var d = date.getDate();
          d = d < 10 ? "0" + d : d;
          let time = y + "-" + m + "-" + d;

          retreat({
            RY_name: this.outData.RY_name,
            RY_degsonid: this.outData.RY_degsonid,
            RY_check_in_address: this.outData.position,
            RY_retreat_type: this.outData.RY_retreat_type,
            RY_remarks: this.outData.RY_remarks,
            Retreat_date: time,
          }).then((res) => {
            if (res) {
              // 提示入住成功
              this.$alert("退宿成功", "系统提示", {
                confirmButtonText: "确定",
                callback: (action) => {
                  console.log("成功");
                },
              });
            }
          });
        }
      });
    },
  },
  mounted() {},
};
</script>